/*

 * Licensed to the Apache Software Foundation (ASF) under one

 * or more contributor license agreements.  See the NOTICE file

 * distributed with this work for additional information

 * regarding copyright ownership.  The ASF licenses this file

 * to you under the Apache License, Version 2.0 (the

 * "License"); you may not use this file except in compliance

 * with the License.  You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package com.bff.gaia.unified.fn.harness.control;



import static com.bff.gaia.unified.vendor.guava.com.google.common.base.Throwables.getStackTraceAsString;



import java.util.EnumMap;

import java.util.Objects;

import java.util.concurrent.BlockingDeque;

import java.util.concurrent.CompletableFuture;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.Executor;

import java.util.concurrent.LinkedBlockingDeque;

import com.bff.gaia.unified.model.fnexecution.v1.UnifiedFnApi;

import com.bff.gaia.unified.model.fnexecution.v1.UnifiedFnControlGrpc;

import com.bff.gaia.unified.model.pipeline.v1.Endpoints.ApiServiceDescriptor;

import com.bff.gaia.unified.sdk.fn.channel.ManagedChannelFactory;

import com.bff.gaia.unified.sdk.fn.stream.OutboundObserverFactory;

import com.bff.gaia.unified.sdk.function.ThrowingFunction;

import com.bff.gaia.unified.vendor.grpc.v1p13p1.io.grpc.Status;

import com.bff.gaia.unified.vendor.grpc.v1p13p1.io.grpc.stub.StreamObserver;

import com.bff.gaia.unified.vendor.guava.com.google.common.util.concurrent.Uninterruptibles;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;



/**

 * A client for the Unified Fn Control API. Uses an unbounded internal queue to pull down an unbounded

 * number of requests.

 *

 * <p>Also can delegate to a set of handlers based upon the {@link

 * UnifiedFnApi.InstructionRequest.RequestCase request type}.

 *

 * <p>When the inbound instruction stream finishes successfully, the {@code onFinish} is completed

 * successfully signaling to the caller that this client will not produce any more {@link

 * UnifiedFnApi.InstructionRequest}s. If the inbound instruction stream errors, the {@code onFinish} is

 * completed exceptionally propagating the failure reason to the caller and signaling that this

 * client will not produce any more {@link UnifiedFnApi.InstructionRequest}s.

 */

public class UnifiedFnControlClient {

  private static final String FAKE_INSTRUCTION_ID = "FAKE_INSTRUCTION_ID";

  private static final Logger LOG = LoggerFactory.getLogger(UnifiedFnControlClient.class);

  private static final UnifiedFnApi.InstructionRequest POISON_PILL =

      UnifiedFnApi.InstructionRequest.newBuilder().setInstructionId(FAKE_INSTRUCTION_ID).build();



  private final StreamObserver<UnifiedFnApi.InstructionResponse> outboundObserver;

  private final BlockingDeque<UnifiedFnApi.InstructionRequest> bufferedInstructions;

  private final EnumMap<

          UnifiedFnApi.InstructionRequest.RequestCase,

	  ThrowingFunction<UnifiedFnApi.InstructionRequest, UnifiedFnApi.InstructionResponse.Builder>>

      handlers;

  private final CompletableFuture<Object> onFinish;



  public UnifiedFnControlClient(

      String id,

      ApiServiceDescriptor apiServiceDescriptor,

      ManagedChannelFactory channelFactory,

      OutboundObserverFactory outboundObserverFactory,

      EnumMap<

              UnifiedFnApi.InstructionRequest.RequestCase,

		  ThrowingFunction<UnifiedFnApi.InstructionRequest, UnifiedFnApi.InstructionResponse.Builder>>

          handlers) {

    this.bufferedInstructions = new LinkedBlockingDeque<>();

    this.outboundObserver =

        outboundObserverFactory.outboundObserverFor(

            UnifiedFnControlGrpc.newStub(channelFactory.forDescriptor(apiServiceDescriptor))::control,

            new InboundObserver());

    this.handlers = handlers;

    this.onFinish = new CompletableFuture<>();

  }



  private static final Object COMPLETED = new Object();



  /**

   * A {@link StreamObserver} for the inbound stream that completes the future on stream

   * termination.

   */

  private class InboundObserver implements StreamObserver<UnifiedFnApi.InstructionRequest> {



    @Override

    public void onNext(UnifiedFnApi.InstructionRequest value) {

      LOG.debug("Received InstructionRequest {}", value);

      Uninterruptibles.putUninterruptibly(bufferedInstructions, value);

    }



    @Override

    public void onError(Throwable t) {

      placePoisonPillIntoQueue();

      onFinish.completeExceptionally(t);

    }



    @Override

    public void onCompleted() {

      placePoisonPillIntoQueue();

      onFinish.complete(COMPLETED);

    }



    /**

     * This method emulates {@link Uninterruptibles#putUninterruptibly} but placing the element at

     * the front of the queue.

     *

     * <p>We place the poison pill at the front of the queue because if the server shutdown, any

     * remaining instructions can be discarded.

     */

    private void placePoisonPillIntoQueue() {

      while (true) {

        try {

          bufferedInstructions.putFirst(POISON_PILL);

          return;

        } catch (InterruptedException e) {

          // Ignored until we place the poison pill into the queue

        }

      }

    }

  }



  /**

   * Note that this method continuously submits work to the supplied executor until the Unified Fn

   * Control server hangs up or fails exceptionally.

   */

  public void processInstructionRequests(Executor executor)

      throws InterruptedException, ExecutionException {

    UnifiedFnApi.InstructionRequest request;

    while (!Objects.equals((request = bufferedInstructions.take()), POISON_PILL)) {

      UnifiedFnApi.InstructionRequest currentRequest = request;

      executor.execute(

          () -> {

            try {

              UnifiedFnApi.InstructionResponse response =

                  delegateOnInstructionRequestType(currentRequest);

              sendInstructionResponse(response);

            } catch (Error e) {

              sendErrorResponse(e);

              throw e;

            }

          });

    }

    onFinish.get();

  }



  public UnifiedFnApi.InstructionResponse delegateOnInstructionRequestType(

      UnifiedFnApi.InstructionRequest value) {

    try {

      return handlers

          .getOrDefault(value.getRequestCase(), this::missingHandler)

          .apply(value)

          .setInstructionId(value.getInstructionId())

          .build();

    } catch (Exception e) {

      LOG.error(

          "Exception while trying to handle {} {}",

          UnifiedFnApi.InstructionRequest.class.getSimpleName(),

          value.getInstructionId(),

          e);

      return UnifiedFnApi.InstructionResponse.newBuilder()

          .setInstructionId(value.getInstructionId())

          .setError(getStackTraceAsString(e))

          .build();

    } catch (Error e) {

      LOG.error(

          "Error thrown when handling {} {}",

          UnifiedFnApi.InstructionRequest.class.getSimpleName(),

          value.getInstructionId(),

          e);

      throw e;

    }

  }



  public void sendInstructionResponse(UnifiedFnApi.InstructionResponse value) {

    LOG.debug("Sending InstructionResponse {}", value);

    outboundObserver.onNext(value);

  }



  private void sendErrorResponse(Error e) {

    onFinish.completeExceptionally(e);

    outboundObserver.onError(

        Status.INTERNAL

            .withDescription(String.format("%s: %s", e.getClass().getName(), e.getMessage()))

            .asException());

    // TODO: Should this clear out the instruction request queue?

  }



  private UnifiedFnApi.InstructionResponse.Builder missingHandler(

      UnifiedFnApi.InstructionRequest request) {

    return UnifiedFnApi.InstructionResponse.newBuilder()

        .setError(String.format("Unknown InstructionRequest type %s", request.getRequestCase()));

  }

}